package com.recharge.web.router;

import com.recharge.web.handler.UserLoginHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

@Configuration
public class RouterFunctionConfig {

    @Autowired
    private UserLoginHandler userLoginHandler;

    @Bean
    public RouterFunction<ServerResponse> route() {
        return RouterFunctions
                .route(RequestPredicates.POST("/user/register")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                        userLoginHandler::register)
                .andRoute(RequestPredicates.POST("/user/login")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                        userLoginHandler::login);
    }
}
